// view/index/jiankong/jiankong.js
let app = getApp()
let yuming = app.globalData.yuming
const request = require('../../config/request');
Page({

  /**
   * 页面的初始数据
   */
  ...request,
  data: {
    yuming: yuming,
    heights: "",
    title: '视频设备配置',
    jian: [],
    isAn: true,
    xufeiprice: '',
    ispaybox: false,
    jinghe_id: '',
    navigatepause:true,
    // jian:[
    //   // {name:'陕西德友·泾河新城第一分店',url:'http://hls01open.ys7.com/openlive/28f79a33e9094a45a526ee958bd36162.hd.m3u8',id:-1},
    //   // {name:'陕西德友·泾河新城第二分店',url:''},
    //   {name:'监控一',url:'http://hls01open.ys7.com/openlive/8e9825179fec48e19b2b6191e9692fbc.hd.m3u8',id:-1},
    //   // {name:'陕西德友·泾河新城第四分店',url:''},
    //   {name:'监控二',url:'http://hls01open.ys7.com/openlive/6c4c61fa74fe400ab60eebc7c37b0954.hd.m3u8',id:-2},
    // ]
  },
  // 泾河新城删除监控
  // 删除员工
  delete(e) {
    var id = e.currentTarget.dataset.id
    var that = this
    wx.showModal({
      title: '您确定要删除该监控吗？',
      cancelText: '否',
      confirmText: '是',
      success(res) {
        if (res.confirm) {
          console.log("是")
          that.shan(id)
        } else {
          console.log("否")
        }
      }
    })
  },
  shan(id) {
    var that = this
    this.myRequest(yuming + 'Monitor/shop_monitor_remove', {
      id: id,
      shopId: wx.getStorageSync('shopId')
    }, "POST").then(res => {
      wx.showToast({
        title: res.data.msg,
        icon: 'none'
      })
      if (res.data.code == 200) {
        setTimeout(function () {
          that.getjiankongList()
        }, 500)
      }
    })
  },
  getjiankongList() {
    wx.showLoading({
      title: '加载中',
      icon: 'none'
    })
    this.myRequest(yuming + 'Monitor/shop_monitor_list', {
      shopId: wx.getStorageSync('shopId')
    }, "POST").then(res => {
      if (res.data.code == 200) {
        wx.hideLoading();
        this.setData({
          jian: res.data.data,
        })
      }
    })
  },
  jinghejia_list() {
    wx.showLoading({
      title: '加载中',
      icon: 'none'
    })
    let deviceSerial = ['F40309451', 'E83523143', 'F40310269', 'F40309633', 'F40310155'];
    deviceSerial.forEach(item => {
      this.myRequest('https://msion.86wky.com/data.php/Jankongnewa/getLiveAddress', {
        shopId: wx.getStorageSync('shopId'),
        deviceSerial: 827902846,
        code: 1
        //      
        // let name = ['陕西德友·泾河新城第一分店','陕西德友·泾河新城第二分店','陕西德友·泾河新城第三分店','陕西德友·泾河新城第四分店','陕西德友·泾河新城第五分店'];
      }, "POST").then(res => {

      })
    })

  },
  backs() {
    wx.navigateBack({
      delta: 0
    })
  },
  // 监控详情
  xiang(e) {
    console.log(e)
    let id = e.currentTarget.dataset.id
    let url = e.currentTarget.dataset.url
    console.log(url)
    wx.navigateTo({
      url: '/page_one/jiankongxq/jiankongxq?id=' + id + "&url=" + url,
    })
  },
  // 申请记录
  sqjl() {
    wx.navigateTo({
      url: '/page_one/shenqjl/shenqjl',
    })
  },
  // 申请监控
  sqjk() {
    wx.showToast({
      title: '设备请自行购买',
      icon:'error'
    })
    // wx.navigateTo({
    //   url: '/page_one/shenqjiank/shenqjiank',
    // })
  },
  // 配置监控
  peizhi(e) {
    let id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/page_one/jiankongpz/jiankongpz?id=' + id,
    })
  },
  shua(e) {
    let id = e.currentTarget.dataset.id
    if (this.data.isAn) {
      this.setData({
        isAn: false
      })
      this.shuaxin(id);
    } else {
      wx.showToast({
        title: '请勿频繁请求',
        icon: 'none'
      })
    }
  },
  // 刷新监控
  shuaxin(id) {
    wx.showLoading({
      title: '刷新中',
      icon: 'none'
    })
    this.myRequest(yuming + 'shop/shop_save_monitor_url', {
      id,
    }, "POST").then(res => {
      wx.hideLoading();
      if (res.data.code == 200) {
        this.getjiankongList();
        setTimeout(() => {
          this.setData({
            isAn: true
          })
        }, 60000)
      }
    })
  },
  // 续费
  queding(e) {
    let id = e.currentTarget.dataset.id
    var that = this
    wx.showModal({
      title: '您确定要续费吗？',
      cancelText: '取消',
      confirmText: '确定',
      success: function (res) {
        if (res.confirm) {
          console.log("确定")
          that.xufei(id)
        } else {
          console.log("取消")
        }
      }
    })
  },
  // 续费监控
  xufei(id) {
    this.myRequest(yuming + 'shop/shop_xufei_monitor', {
      shopId: wx.getStorageSync('shopId'),
      id
    }, 'POST').then(res => {
      if (res.data.code == 200) {
        let orderSn = res.data.data.orderSn
        this.setData({
          ispaybox: true,
          xufeiprice: res.data.data.payPrice
        })
        setTimeout(() => {
          this.setData({
            ispaybox: false
          })
          this.pay(orderSn);
        }, 3000)
      }
    })
  },
  // 模拟支付
  pay(orderSn) {
    this.myRequest(yuming + 'Notify/pay_notify', {
      out_trade_no: orderSn
    }, 'POST').then(res => {
      wx.hideLoading();
      wx.showToast({
        title: res.data.msg,
        icon: 'none'
      })
      if (res.data.code == 200) {
        this.getjiankongList();
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      heights: wx.getMenuButtonBoundingClientRect().top + 5,
      title: options.title
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  // 添加设备
  tianjia_she() {
    wx.navigateTo({
      url: '/page_one/addjiankong/addjiankong',
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      jinghe_id: wx.getStorageSync('jinghe_id')
    })
    // if (this.data.jinghe_id != 176) {
    this.getjiankongList();
    // }
    // if (this.data.jinghe_id == 176) {
    // this.jinghejia_list(); 
    // }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
})